<?php



declare (strict_types=1);

namespace app\data\command;

use think\admin\Command;
use app\data\model\DataDevice;
use app\data\service\DeviceService;
/**
 * 同步设备
 * @class Device
 * @package app\data\command
 */
class Device extends Command
{
    /**
     * 配置指令
     */
    protected function configure()
    {
        $this->setName('Aliyun:syncDevice');
        $this->setDescription('syncDevice Data');
    }

    /**
     * 任务执行处理
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\admin\Exception
     */
    public function handle()
    {
        $this->test();
    }

    /**
     * 同步设备列表
     * @param string $next
     * @param integer $done
     * @return string
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\admin\Exception
     */
    protected function test()
    {
        $next = '';
        $done = 0;
        $deviceObj = new DeviceService();
        $this->process->message('开始获取设备数据');
        while (is_string($next)) {
            $result = $deviceObj->QueryDevice($next);
            $res = $result['data'];
            if ($result['err_code'] == 0 && $res->body->code =='') {
                $deviceInfo = $res->body->data->deviceInfo;
                foreach($deviceInfo as $val){
                    if($val->deviceStatus == 'ONLINE'){
                        $status = 1;
                    }elseif($val->deviceStatus == 'OFFLINE'){
                        $status = 0;
                    }elseif($val->deviceStatus == 'UNACTIVE'){
                        $status = -1;
                    }else{
                        $status = -2;
                    }
                    $data=[
                        'deviceName'=>$val->deviceName,
                        'deviceSecret'=>$val->deviceSecret,
                        'iotId'=>$val->iotId,
                        'status'=>$status,
                        'nickname'=>isset($val->nickname)?$val->nickname:'',
                        'productKey'=>config('iot.ProductKey')
                    ];
                    $this->queue->message($res->body->total, ++$done, "-> 开始获取 {$val->deviceName}");
                    $check= DataDevice::mk()->where(['iotId'=>$data['iotId']])->count();
                    if($check>0){
                        DataDevice::mk()->where(['iotId'=>$data['iotId']])->update($data);
                    }else {
                        DataDevice::mk()->add($data);
                    }
                    $this->queue->message($res->body->total, $done, "-> 完成更新 {$val->deviceName}", 1);
                }
                $next = $res->body->total > $done ? $res->body->nextToken : null;
            } else {
                $next = null;
            }
        }
        $this->process->message($done > 0 ? '设备数据获取完成' : '未获取到设备数据');
        $this->process->message('');
        return "共获取 {$done} 个设备数据";
    }
}
